<template>
	<view class="page">
		<custom-navigation :title="title" rightTxt="保存" @rightAction="save"></custom-navigation>

		<scroll-view class="body-scroll" scroll-y="true">
			<!-- <picker @change="selectAssetNum" :range="classifyNames" :value="taskindex">
				<view v-if="isParent" class="item flex-row-center">
					<view class="title">上级分类</view>
					<view :class="parentData.classify_name ?'value':'hint-input'">
						{{parentData.classify_name || '为空时创建一级分类'}}</view>
					<image src="../../../static/arrow-blackmin.png"></image>
				</view>
			</picker> -->
			<view class="line" v-if="isParent"></view>
			<view class="item flex-row-center">
				<view class="title">单位名称<text class="x">*</text></view>
				<input v-model="name"  placeholder="请输入单位名称" placeholder-class="hint-input" />
			</view>


		</scroll-view>

		<view class="bottom-container flex-row-center" v-if="currentNo">
			<view class="item1" @click="dialogShow=true">删除</view>
		</view>
		<alert :show="dialogShow" message="是否删除!" @leftAction="dialogShow=false" @rightAction="del"></alert>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: "新增单位",
				isParent: true,
				parentData: {},
				name: '',
				classifycations: [],
				classifyNames: [],
				taskindex: 0,
				currentNo: null,
				id: 0,
				selectMode: false,
				dialogShow:false,
			}
		},
		onLoad(options) {
			this.selectMode = options.selectMode ?? false
			this.isParent = options.isParent ?? true
			this.currentNo = options.currentNo;
			this.id = options.id;
			let parentDatajson = options.parentData;
			this.title = this.currentNo == null ? "新增单位" : "编辑单位"
			if (parentDatajson != null) {
				this.parentData = JSON.parse(parentDatajson)
			}
			this.name = options.name

		},
		methods: {

			del() {

				uni.showLoading({
					title: "删除中..."
				})
				this.$apiPost("/retail/select/srvretail_goods_info_select", {
					serviceName: 'srvretail_goods_info_select',
					condition: [{
						"colName": "goods_unit",
						"ruleType": "like",
						"value": this.currentNo
					}],
					page: {
						"pageNo": 1,
						"rownumber": 1
					},
					"colNames": ["*"]
				}).then(res => {
					if (res.data.length > 0) {
						this.$showToast("该单位下有商品，不能删除");
						uni.hideLoading()
						return;
					} else {
						this.$apiPost("/retail/operate/srvretail_store_unit_delete", [{
							"serviceName": "srvretail_store_unit_delete",
							"condition": [{
								"colName": "id",
								"ruleType": "in",
								"value": this.id
							}]
						}]).then(res => {
							uni.hideLoading()
							if (res.state == 'SUCCESS') {
								uni.showToast({
									title: "删除成功！",
									mask: true
								})
								
								setTimeout(() => {
									uni.setStorageSync("refresh", true)
									uni.navigateBack()
								}, 1500)
							}
						})
					}
				})






			},
			save() {
				if (!this.name || this.name.length == 0) {
					uni.showToast({
						title: "名称不能为空",
						icon: 'none'
					})
					return;
				}
				if(this.name.indexOf(' ')!=-1){
					uni.showToast({
						title: "名称不能包含空格!",
						icon: 'none'
					})
					return;
				}
				uni.showLoading({
					title: "加载中..."
				})
				this.$apiPost("/retail/select/srvretail_store_unit_select", {
					"serviceName": "srvretail_store_unit_select",
					"colNames": ["*"],
					"condition": [{
						"colName": "unit_name",
						"ruleType": "eq",
						"value": (this.name||'').trim()
					}],
					"order": [],
					"page": {
						"pageNo": 1,
						"rownumber": 1
					}
				}).then(res => {
					if (res.data.length > 0) {
						if(res.data.length==1 && res.data[0].id==this.id){
							uni.hideLoading()
							uni.showToast({
								title: "保存成功！",
								mask: true
							})
							uni.setStorageSync("refresh", true)
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						} else {
							this.$showToast("已存在该名称！")
							uni.hideLoading()
						}
						return;
					}
					this.$apiPost(this.currentNo ? "/retail/operate/srvretail_store_unit_update" :
						"/retail/operate/srvretail_store_unit_add", [{
							"serviceName": this.currentNo ? "srvretail_store_unit_update" :
								"srvretail_store_unit_add",
							"condition": this.currentNo ? [{
								"colName": "id",
								"ruleType": "eq",
								"value": this.id
							}] : [],
							"data": [{
								"unit_name": (this.name||'').trim()
							}]
						}]).then(res => {
						uni.hideLoading()
						uni.showToast({
							title: "保存成功！",
							mask: true
						})
						uni.setStorageSync("refresh", true)
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					})
				})


			},
			selectAssetNum(e) {
				this.parentData = this.classifycations[e.detail.value];
			},

		}
	}
</script>

<style scoped lang="scss">
	.line {
		width: 100%;
		height: 2rpx;
		opacity: 1;
		margin: 0rpx 24rpx;
		background-color: rgba(245, 245, 247, 1);


	}

	.item {
		padding: 28rpx 24rpx;
		background-color: white;

		.title {
			font-size: 32rpx;
			font-weight: 700;

			.x {
				color: rgba(212, 48, 48, 1);
			}
		}

		input {
			flex: 1;
			font-size: 30rpx;
			font-weight: 500;
			text-align: end;
		}

		.hint-input {
			text-align: end;
			flex: 1;
			font-size: 30rpx;
			font-weight: 400;
			color: rgba(153, 153, 153, 1);
		}

		image {
			width: 8rpx;
			height: 16rpx;
			margin-left: 12rpx;
		}

		.value {
			flex: 1;
			text-align: end;
			font-size: 30rpx;
			font-weight: 500;
			color: rgba(51, 51, 51, 1);
		}
	}

	.bottom-container {
		background-color: white;
		box-shadow: 0 -16rpx 16rpx 1rpx #00000008;
		height: 146rpx;
		padding: 0 24rpx;
		justify-content: space-around;

		.item {
			border-radius: 40rpx;
			line-height: 80rpx;
			text-align: center;
			border: 2rpx solid #333333FF;
			width: 192rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-weight: 500;

		}

		.item1 {
			border-radius: 40rpx;
			line-height: 80rpx;
			text-align: center;
			border: 2rpx solid rgba(56, 192, 158, 1);
			background-color: rgba(56, 192, 158, 1);
			color: white;
			width: 320rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-weight: 500;
		}

		.text {
			flex: 1;
			margin-left: 60rpx;
			font-size: 30rpx;
			font-weight: 700;

		}

		.btn {
			margin-right: 34rpx;
			width: 192rpx;
			height: 80rpx;
			border-radius: 40rpx;
			background: rgba(56, 192, 158, 1);
			font-size: 30rpx;
			font-weight: 700;
			color: white;
			text-align: center;
			line-height: 80rpx;
		}
	}
</style>
